<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
     function fn(nums) {
      if(nums.length === 0) return null;
      // apply()：调用一个具有给定 this 值的函数，以及以一个数组（或一个类数组对象）的形式提供的参数
      // Math.max.apply() 获取nums数组中最大的值
      let max = Math.max.apply(null, nums);
      let index = nums.indexOf(max);
      // new TreeNode()：创建一个树节点
      let root = new TreeNode(max);
      let left = nums.slice(0, index);
      let right = nums.slice(index + 1);
      root.left = fn(left);
      root.right = fn(right);
      return root
    }
    console.log(fn([3,2,1,6,0,5]))
  </script>
</body>
</html>